﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ARR.Entities;
using System.IO;
using ARR.Entities.Gateways;
using OfficeOpenXml;
using ARR.DataModel.Repositories;
using OfficeOpenXml.Style;
using System.Drawing;

namespace ARR.Services.Domain.Exporters
{
    public static class StoreMasterExport
    {
        public static void ExportStoreMaster()
        {
            Country country = CountriesDomain.GetByCode(Constants.CountryCode.Canada);

            FileInfo newFile = ExportHelper.CreateExcelFile("StoreMaster.xlsx");
            List<TempStore> stores = ExportHelper.GetStoresToExport(country);
            
            using (ExcelPackage package = new ExcelPackage(newFile))
            {
                // add a new worksheet to the empty workbook
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Store Master");
                //Add the headers
                worksheet.Cells[1, 1].Value = "Code";
                worksheet.Cells[1, 2].Value = "Name";
                worksheet.Cells[1, 3].Value = "Region";
                worksheet.Cells[1, 4].Value = "City";
                worksheet.Cells[1, 5].Value = "Address";
                worksheet.Cells[1, 6].Value = "Phone";
                worksheet.Cells[1, 7].Value = "Fax";
                worksheet.Cells[1, 8].Value = "Format";

                //Freeze headers
                worksheet.View.FreezePanes(2, 1);


                TempStore store;
                int row = 0;
                for (int i = 0; i < stores.Count; i++)
                {
                    row = i + 2;
                    store = stores[i];

                    worksheet.Cells[row, 1].Value = store.Code;
                    worksheet.Cells[row, 2].Value = store.Name;
                    worksheet.Cells[row, 3].Value = store.Region;
                    worksheet.Cells[row, 4].Value = store.City;
                    worksheet.Cells[row, 5].Value = store.Address;
                    worksheet.Cells[row, 6].Value = store.Phone;
                    worksheet.Cells[row, 7].Value = store.Fax;
                    worksheet.Cells[row, 8].Value = store.Format;
                }


                //Styling Cells
                using (var range = worksheet.Cells[1, 1, 1, 8])
                {
                    range.Style.Font.Bold = true;
                    range.Style.Fill.PatternType = ExcelFillStyle.Solid;
                    range.Style.Fill.BackgroundColor.SetColor(Color.DarkBlue);
                    range.Style.Font.Color.SetColor(Color.White);
                }


                //Create an autofilter for the range
                worksheet.Cells["A1:H1"].AutoFilter = true;

                // save our new workbook and we are done!
                package.Save();
            }
        }
    }
}
